package com.ruoyi.produce.base.bom.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.machine.base.device.domain.Device;
import com.ruoyi.produce.base.bom.domain.Bom;
import com.ruoyi.produce.base.bom.domain.ProductionMachine;
import com.ruoyi.produce.base.bom.mapper.BomMapper;
import com.ruoyi.produce.base.mould.domain.Mould;
import com.ruoyi.produce.base.procedure.domain.Procedure;
import com.ruoyi.psi.base.product.domain.ProductCond;
import com.ruoyi.psi.base.product.domain.ProductDto;
import com.ruoyi.psi.base.product.mapper.ProductMapper;
import com.ruoyi.psi.base.product.service.IProductService;
import com.ruoyi.utils.UtilsList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 产品结构
 * 业务执行层
 * @author Drj 2021/6/28
 */
@Service
public class BomServiceImpl implements IBomService {
    @Autowired
    private BomMapper bomMapper;

    @Autowired
    private IBomService bomService;

    @Autowired
    private ProductMapper productMapper;


    /**
     * 获取存在Bom的所有产品
     *
     * @return
     */
    @Override
    public List<ProductDto> selectProduceForBom(ProductCond cond) {
//        if (StringUtils.isNull(baseProduce.getProduceId())){
//            return null;
//        }
//        List<ProductDto> productDtos = productService.selectProductList(cond);
        List<ProductDto> productDtos = productMapper.selectProductList(cond);
//        int i = 0;
//        for (; i < productDtos.size();) {
//            if (StringUtils.isNull(bomMapper.selectBom(productDtos.get(i).getProduceId()))){
//                //不存在对应产品结构，则移除对应产品，同时此时i值不增加
//                productDtos.remove(i);
//                continue;
//            }
//            i++;
//        }
        return productDtos;
    }

    /**
     * 查找Bom
     *
     * @param produceId 产品编号
     * @return
     */
    @Override
    public Bom selectBom(String produceId) {
        Bom bom = bomMapper.selectBom(produceId);
        if (bom == null) {
            // 无bom结构对象
            return new Bom();
        }
        if (StringUtils.isNotEmpty(bomMapper.selectProcedureList(produceId))){
            bom.setProcedureList(bomMapper.selectProcedureList(produceId));//存入对应的工序
        }
        if (StringUtils.isNotEmpty(bomMapper.selectMachineList(produceId))){
            bom.setDeviceList(bomMapper.selectMachineList(produceId));//存入对应的设备
        }
        if (StringUtils.isNotEmpty(bomMapper.selectMouldList(produceId))){
            bom.setMouldList(bomMapper.selectMouldList(produceId));//存入对应的模具
        }
        bom.setProduceChild(selectBomChild(bom));
        return bom;
    }

    /**
     * 存入子件
     * 递归
     * @param bom
     * @return
     */
    public List<Bom> selectBomChild(Bom bom){
        List<Bom> bomList = bomMapper.selectBomChildList(bom.getProduceId());
        for (int i = 0; i < bomList.size(); i++) {
            bomList.get(i).setProcedureList(bomMapper.selectProcedureList(bomList.get(i).getProduceId()));//存入子件对应的工序
            bomList.get(i).setDeviceList(bomMapper.selectMachineList(bomList.get(i).getProduceId()));//存入子件对应的设备
            bomList.get(i).setMouldList(bomMapper.selectMouldList(bomList.get(i).getProduceId()));//存入子件对应的模具
            if (StringUtils.isNotNull(bomMapper.selectBomChildList(bomList.get(i).getProduceId()))){
                //子件存在子件，则再次调用该方法存入子件
                bomList.get(i).setProduceChild(selectBomChild(bomList.get(i)));
            }
        }
        return bomList;
    }


    /**
     * 新增Bom
     *
     * @param bom
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertBom(Bom bom) throws Exception {
        if (StringUtils.isNotNull(bomMapper.selectBom(bom.getProduceId()))){
            //存在
            return 0;
        }
        //不存在
        List<Bom> bomList = bom.getProduceChild();
        bom.setProduceChild(null);
        bomMapper.insertBom(bom);
        for (int i = 0; i < bomList.size(); i++) {
            bomMapper.insertBomChild(bomList.get(i),bom);
        }
        //避免无工序状态
        if (StringUtils.isNotNull(bom.getProcedureList())){
            for (int i = 0; i < bom.getProcedureList().size(); i++) {
                //新增产品对应工序
                bomMapper.insertBomProcedure(bom.getProduceId(),bom.getProcedureList().get(i).getProcedureId());
            }
        }
        //避免无设备状态
        if (StringUtils.isNotNull(bom.getDeviceList())){
            for (int i = 0; i < bom.getDeviceList().size(); i++) {
                //新增产品对应设备
                bomMapper.insertBomMachine(bom.getProduceId(),bom.getDeviceList().get(i).getMachineId(), bom.getDeviceList().get(i).getCapacity());
            }
        }
        //避免无模具状态
        if (StringUtils.isNotNull(bom.getMouldList())){
            for (int i = 0; i < bom.getMouldList().size(); i++) {
                //新增产品对应模具
                bomMapper.insertBomMould(bom.getProduceId(),bom.getMouldList().get(i).getMouldId());
            }
        }
        return 1;
    }

    /**
     * 更新Bom
     *
     * @param bom
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateBom(Bom bom) throws Exception {
        if (!StringUtils.isNotNull(bomMapper.selectBom(bom.getProduceId()))){
            //不存在
            return bomService.insertBom(bom);
        }
        //存在
        List<Bom> bomListBefore = bomMapper.selectBomChildList(bom.getProduceId());
        List<Bom> bomList = bom.getProduceChild();
        bom.setProduceChild(null);
        bomMapper.updateBom(bom);
        for (int i = 0; i < bomList.size(); i++) {
            Bom bom1 = bomMapper.selectBomChildById(bomList.get(i).getProduceId(),bom.getProduceId());
            if (StringUtils.isNotNull(bom1)){
                //已存在
                for (int j = 0; j < bomListBefore.size(); j++) {
                    if (bomListBefore.get(j).getProduceId().equals(bom1.getProduceId())){
                        bomListBefore.remove(j);
                        break;
                    }
                }
                //更新
                bomMapper.updateBomChild(bomList.get(i),bom);
            }else{
                //不存在，新增
                bomMapper.insertBomChild(bomList.get(i),bom);
            }

        }
        for (int i = 0; i < bomListBefore.size(); i++) {
            bomMapper.deleteBomChild(bomListBefore.get(i).getProduceId(),bom.getProduceId());//删除原有部分
        }
        bomMapper.deleteBomProcedure(bom.getProduceId());//删除对应产品工序
        bomMapper.deleteBomMachine(bom.getProduceId());//删除对应产品设备
        bomMapper.deleteBomMould(bom.getProduceId());//删除对应产品模具
        //避免无工序状态
        if (StringUtils.isNotNull(bom.getProcedureList())){
            for (int i = 0; i < bom.getProcedureList().size(); i++) {
                //新增产品对应工序
                bomMapper.insertBomProcedure(bom.getProduceId(),bom.getProcedureList().get(i).getProcedureId());
            }
        }
        //避免无设备状态
        if (StringUtils.isNotNull(bom.getDeviceList())){
            for (int i = 0; i < bom.getDeviceList().size(); i++) {
                //新增产品对应设备
                bomMapper.insertBomMachine(bom.getProduceId(),bom.getDeviceList().get(i).getMachineId(), bom.getDeviceList().get(i).getCapacity());
            }
        }
        //避免无模具状态
        if (StringUtils.isNotNull(bom.getMouldList())){
            for (int i = 0; i < bom.getMouldList().size(); i++) {
                //新增产品对应模具
                bomMapper.insertBomMould(bom.getProduceId(),bom.getMouldList().get(i).getMouldId());
            }
        }
        return 1;
    }

    /**
     * 删除Bom
     * 仅修改删除标志
     *
     * @param bom
     * @return
     */
    @Override
    public int updateBomDeleteFlag(Bom bom) {
        return bomMapper.updateBomDeleteFlag(bom);
    }

    /**
     * 检查是否存在子父构件循环，不能保存
     * 新增/修改添加子件时检验
     * @param bom
     * @return
     */
    @Override
    public boolean checkBomParent(Bom bom) {
        List<Bom> bomList = UtilsList.getBomList(bom);
        for (int i = 1; i < bomList.size(); i++) { //跳过其中父件
            //查找当前子件产品所包含的子件信息,若存在子件时
            if (StringUtils.isNotNull(selectBom(bomList.get(i).getProduceId()))){
                List<Bom> bomTemp = UtilsList.getBomList(selectBom(bomList.get(i).getProduceId()));
                for (int j = 0; j < bomTemp.size(); j++) {
                    //若该产品子件中包含父件，则返回0
                    if (bomTemp.get(j).getProduceId().equals(bom.getProduceId())){
                        return false;
                    }
                }
            }
        }

        return true;
    }

    /**
     * 新增时查验是否已存在对应产品结构清单
     * 选中父件产品时便检验
     * @param bom
     * @return
     */
    @Override
    public boolean checkBomExist(Bom bom) {
        if (StringUtils.isNotNull(bomMapper.selectBom(bom.getProduceId()))){
            return false;
        }
        return true;
    }

    /**
     * 查询产品结构对应的工序
     *
     * @param produceId
     * @return
     */
    @Override
    public List<Procedure> selectProcedureList(String produceId) {
        return bomMapper.selectProcedureList(produceId);
    }

    /**
     * 查询产品结构对应的设备
     *
     * @param produceId
     * @return
     */
    @Override
    public List<ProductionMachine> selectMachineList(String produceId) {
        return bomMapper.selectMachineList(produceId);
    }

    /**
     * 查询产品结构对应的模具
     * @param produceId
     * @return
     */
    @Override
    public List<Mould> selectMouldList(String produceId) {
        return bomMapper.selectMouldList(produceId);
    }

    /**
     * 删除父件对应的指定子件
     * @param produceId
     * @param produceParentId
     * @return
     */
    @Override
    public int deleteBomChild(String produceId, String produceParentId) {
        return bomMapper.deleteBomChild(produceId, produceParentId);
    }
}
